package com.ruoyi.factorydata.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.CraftProcessMaterialRelation;
import com.ruoyi.factorydata.domain.MaterialRequisitionDetail;
import com.ruoyi.factorydata.domain.WorkOrderInfo;
import com.ruoyi.factorydata.domain.dto.ProductStatisticsDTO;
import com.ruoyi.factorydata.domain.dto.WorkOrderStatisticsDTO;
import com.ruoyi.factorydata.service.IWorkOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工单信息Controller
 *
 * @author ruoyi
 * @date 2025-07-27
 */
@RestController
@RequestMapping("/workOrder/workOrderInfo")
public class WorkOrderInfoController extends BaseController {
    @Autowired
    private IWorkOrderInfoService workOrderInfoService;

    /**
     * 查询工单信息列表
     */
    @PreAuthorize("@ss.hasPermi('statistics:workOrder:view')")
        @GetMapping("/statistics/list")
        public TableDataInfo statisticsList(WorkOrderInfo workOrderInfo) {
            startPage();
            List<WorkOrderStatisticsDTO> list = workOrderInfoService.statisticsList(workOrderInfo);
            return getDataTable(list);
    }
    /**
     * 查询工单信息列表
     */
    @PreAuthorize("@ss.hasPermi('statistics:workOrder:view')")
    @GetMapping("/statistics/product/list")
    public TableDataInfo statisticsProductList(WorkOrderInfo workOrderInfo) {
        startPage();
        List<ProductStatisticsDTO> list = workOrderInfoService.statisticsProductList(workOrderInfo);
        return getDataTable(list);
    }
    /**
     * 查询工单信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrderInfo workOrderInfo) {
        startPage();
        List<WorkOrderInfo> list = workOrderInfoService.selectWorkOrderInfoList(workOrderInfo);
        return getDataTable(list);
    }

    /**
     * 查询工单信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:task')")
    @GetMapping("/taskList")
    public TableDataInfo listTask(WorkOrderInfo workOrderInfo) {
        startPage();
        List<WorkOrderInfo> list = workOrderInfoService.selectWorkOrderInfoTaskList(workOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出工单信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:export')")
    @Log(title = "工单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrderInfo workOrderInfo) {
        List<WorkOrderInfo> list = workOrderInfoService.selectWorkOrderInfoList(workOrderInfo);
        ExcelUtil<WorkOrderInfo> util = new ExcelUtil<WorkOrderInfo>(WorkOrderInfo.class);
        util.exportExcel(response, list, "工单信息数据");
    }

    /**
     * 获取工单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(workOrderInfoService.selectWorkOrderInfoById(id));
    }

    /**
     * 新增工单信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:add')")
    @Log(title = "工单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrderInfo workOrderInfo) {
        return toAjax(workOrderInfoService.insertWorkOrderInfo(workOrderInfo));
    }

    /**
     * 修改工单信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:edit')")
    @Log(title = "工单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrderInfo workOrderInfo) {
        return toAjax(workOrderInfoService.updateWorkOrderInfo(workOrderInfo));
    }

    /**
     * 工单开工
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:start')")
    @Log(title = "工单开工", businessType = BusinessType.UPDATE)
    @PutMapping("/start/{id}")
    public AjaxResult start(@PathVariable("id") Long id) {
        return toAjax(workOrderInfoService.startWorkOrderInfo(id));
    }

    /**
     * 工单暂停
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:suspend')")
    @Log(title = "工单暂停", businessType = BusinessType.UPDATE)
    @PutMapping("/suspend/{id}")
    public AjaxResult suspend(@PathVariable("id") Long id) {
        return toAjax(workOrderInfoService.suspendWorkOrderInfo(id));
    }
    /**
     * 工单取消暂停
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:suspend')")
    @Log(title = "工单暂停", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/suspend/{id}")
    public AjaxResult cancelSuspend(@PathVariable("id") Long id) {
        return toAjax(workOrderInfoService.cancelSuspendWorkOrderInfo(id));
    }
    /**
     * 获取工单需要物料信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:applyMaterial')")
    @GetMapping("/getMaterial/{id}")
    public AjaxResult getMaterial(@PathVariable("id") Long id) {
        return success(workOrderInfoService.getMaterial(id));
    }

    /**
     * 删除工单信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:remove')")
    @Log(title = "工单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(workOrderInfoService.deleteWorkOrderInfoByIds(ids));
    }

    /**
     * 新增领料单
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:applyMaterial')")
    @Log(title = "领料单", businessType = BusinessType.INSERT)
    @PostMapping("applyMaterial/{id}")
    public AjaxResult applyMaterial(@PathVariable("id") Long id,
                                    @RequestBody List<CraftProcessMaterialRelation> materialRequisition) {
        return toAjax(workOrderInfoService.applyMaterial(id, materialRequisition));
    }


    /**
     * 完成工序
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:start')")
    @Log(title = "完成工序", businessType = BusinessType.UPDATE)
    @PutMapping("/next/{id}")
    public AjaxResult next(@PathVariable("id") Long id,
                           @RequestParam(value = "actualQuantity", required = false) Integer actualQuantity,
                           @RequestParam(value = "scrapQuantity", required = false) Integer scrapQuantity) {
        return toAjax(workOrderInfoService.next(id, actualQuantity,scrapQuantity));
    }

    /**
     * 工单完工校验
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:start')")
    @Log(title = "工单完工校验", businessType = BusinessType.UPDATE)
    @PutMapping("/validate/{id}")
    public AjaxResult finishValidate(@PathVariable("id") Long id) {
        return success(workOrderInfoService.finishValidate(id));
    }
    /**
     * 工单转移负责人
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:transferWorkOrderOwner')")
    @Log(title = "工单转移负责人", businessType = BusinessType.UPDATE)
    @PutMapping("/transferOwner/{id}")
    public AjaxResult transferOwner(@PathVariable("id") Long id,
                                   @RequestParam(value = "workOrderOwnerId") Long workOrderOwnerId) {
        return toAjax(workOrderInfoService.transferOwner(id, workOrderOwnerId));
    }

    /**
     * 工序转移负责人
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:transferProcessOwner')")
    @Log(title = "工序转移负责人", businessType = BusinessType.UPDATE)
    @PutMapping("/transferProcessOwner/{id}")
    public AjaxResult transferProcessOwner(@PathVariable("id") Long id,
                                           @RequestParam(value = "processOwnerId") Long processOwnerId) {
        return toAjax(workOrderInfoService.transferProcessOwner(id, processOwnerId));
    }

    /**
     * 退料
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:applyMaterial')")
    @Log(title = "退料", businessType = BusinessType.UPDATE)
    @PostMapping("/returnMaterial/{id}")
    public AjaxResult returnMaterial(@PathVariable("id") Long id,
                                     @RequestBody MaterialRequisitionDetail materialRequisitionDetail) {
        return toAjax(workOrderInfoService.returnMaterial(id, materialRequisitionDetail));
    }

    /**
     * 关停工单
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:remove')")
    @Log(title = "关停工单", businessType = BusinessType.DELETE)
    @PostMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable Long id) {
        return toAjax(workOrderInfoService.cancel(id,"workOrder"));
    }


    /**
     * 打印工单
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:query')")
    @GetMapping("/printWorkOrder/{id}")
    public AjaxResult printWorkOrder(@PathVariable Long id) {
        return success(workOrderInfoService.printWorkOrder(id));
    }

}
